package java_12_20;

/**
 * 多线程的优势
 *
 * 采用单线程和多线程
 */
public class Demo3 {
    //以下划线分割数字
    private static long count=10_0000_0000;
    //private static long count=10_00;
    public static void main(String[] args) throws InterruptedException {
        single();
        multi();
    }
    //多线程
    private static void multi() throws InterruptedException {
        long start=System.currentTimeMillis();
        Thread t1=new Thread(()->{
            long a=0;
            for(long i=0;i<count;i++){
                a++;
            }
        });
        t1.start();
        Thread t2=new Thread(()->{
            long b=0;
            for(long i=0;i<count;i++){
                b++;
            }
        });
        t2.start();

        t1.join();
        t2.join();
        long end=System.currentTimeMillis();
        System.out.println("多线程消耗时间："+(end-start)+"ms");
    }
    /**
     * 单线程
     */
    private static void single(){
        long start = System.currentTimeMillis();
        long a=0;
        long b=0;
        for (long i=0;i<count;i++){
            a++;
        }
        for (long i=0;i<count;i++){
            b++;
        }
        //获取当前系统的时间戳
        long end=System.currentTimeMillis();
        System.out.println("单线程消耗时间:"+(end-start)+"ms");
    }
}
